<?php

class Crud_Form_Element_Column_Date extends Crud_Form_Element_Column
{
	public function init()
	{
		$element = $this->getElement();
		$element->addFilter(new Filter_StringToTime());
		$element->setAttrib('class', 'date');
	}
	
	/**
	 * Set value for given row.
	 * 
	 * @param Crud_Section_Row
	 * @param mixed $value
	 * @return void
	 */
	public function setRowValue(Crud_Section_Row $row, $value)
	{
		if (is_numeric($value)) {
			$value = date('Y-m-d H:i:s', $value);
		}
		
		parent::setRowValue($row, $value);
	}
	
	/**
	 * Get default value by row.
	 * 
	 * @param Crud_Section_Row
	 * @return void
	 */
	public function getRowValue(Crud_Section_Row $row = null)
	{
		$value = parent::getRowValue($row);
		$value = preg_replace('/(\d{4})\-(\d{2})\-(\d{2})/', "$3.$2.$1", $value);
		return $value;
	}
}